
/* 引入资源请求插件 */
import axios from 'axios';
import qs from 'qs';

// Vue.prototype.GLOBAL = global_ //挂载到Vue实例上面
const Axios = axios.create({
   baseURL: "/apis", // 因为我本地做了反向代理
  // baseURL: 'https://dudu.gaoxiao.love/healthqrcode/website',
  timeout: 10000,
  responseType: 'json',
  withCredentials: true, // 是否允许带cookie这些
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
});

// 传参序列化
Axios.interceptors.request.use(
  config => {
    //   if (config.method === 'post' || config.method === 'PUT' || config.method === 'DELETE') {
    config.data = qs.stringify(config.data);
    const token = localStorage.getItem('ktoken');
    //  }
    if (token) {
      config.headers['Authorization'] = token;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 返回状态判断
Axios.interceptors.response.use(
  res => {
    if (res.status == '401') {
      localStorage.removeItem('ktoken');
      window.location.href = '/';
    }
    if (res.data && res.status != '200') {
      return Promise.reject(res);
    }
    return res;
  },
  error => {
    // 404等问题可以在这里处理

    return Promise.reject(error);
  }
);
// 进行封装处理
const request = (url, method, params) => {
  return new Promise((resolve, reject) => {
    if (method == 'post') {
      Axios.post(url, params)
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    } else if (method == 'get') {
      Axios.get(url, {
        params: params
      })
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    } else if (method == 'put') {
      Axios.put(url, params)
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    } else if (method == 'delete') {
      Axios.delete(url, {
        data: params
      })
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    }
  });
};

export default {
  login: data => {
    return request('/website/auth/login', 'post', data);
  },
  logout: data => {
    return request('/website/household', 'delete', data);
  },
  updatePsw: data => {
    return request('/website/auth/password', 'put', data);
  },
  residentList: data => {
    return request('/website/household', 'get', data);
  },
  exportResident: data => {
    return request('/website/household/export', 'get', data);
  },
  deleteResident: data => {
    return request('/website/household', 'delete', data);
  },
  residentDeatil: data => {
    return request('/website/household/get', 'get', data);
  },

  exportStaff: data => {
    return request('/website/inspector/export', 'get', data);
  },
  addStaff: data => {
    return request('/website/inspector', 'post', data);
  },
  updateStaff: data => {
    return request('/website/inspector', 'put', data);
  },
  deleteStaff: data => {
    return request('/website/inspector', 'delete', data);
  },
  staffDeatil: data => {
    return request('/website/inspector/get', 'get', data);
  },
  staffList: data => {
    return request('/website/inspector', 'get', data);
  },
  accessStatistic: data => {
    return request('/website/inspector/accessStatistic', 'get', data);
  },
  tempStatistic: data => {
    return request('/website/inspector/temperatureStatistic', 'get', data);
  },
  userHealthInfoList: data => {
    return request('/website/inspector/accessList', 'get', data);
  },
  describeCaptchaResult: data => {
    return request('/website/auth/describeCaptchaResult', 'get', data);
  },
  userVerify: data => {
    return request('/website/household/verify', 'get', data);
  },
  updateConfirmedStatus: data => {
    return request('/website/household/updateConfirmedStatus', 'put', data);
  }
};
